<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"
    import="reports.Report, java.util.*, java.text.DecimalFormat;"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
#pid {width:5em;}
#prodDesc {width:20em;}
#quantity {width:5em;}
#price {width: 5em;}
#total {width: 5em;}
td {padding:.25em;}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sales Report</title>
</head>
<body>
<a href="/IMS/prodType">Product Type</a>&nbsp;&nbsp;
    <a href="/IMS/product">Product </a>&nbsp;&nbsp;
    <a href="/IMS/searchCustOrder">Search Customer Order</a>&nbsp;&nbsp;
    <a href="/IMS/account.jsp">Update Accounts</a></br></br>
<%! 
	double roundTwoDecimals(double d) {
	DecimalFormat twoDForm = new DecimalFormat("#.##");
	return Double.valueOf(twoDForm.format(d));
}
%>

<%

//load cookies
	Cookie[] cookies = request.getCookies();
	Cookie c = null; 
	HashMap<String, String> reportParam = new HashMap<String, String>(); 

	for (int i = 0; i < cookies.length; i++) {
		c = cookies[i]; 
		if (reportParam.containsKey(c.getName()) == false) 
			reportParam.put(c.getName(), c.getValue()); 
	}

	String report = reportParam.get("report"); 
	String period = reportParam.get("period"); 
	String startDate = reportParam.get("startDate"); 
	String startMonth = reportParam.get("startMonth");  
	String startYear = reportParam.get("startYear"); 
	String endDate = reportParam.get("endDate"); 
	String endMonth = reportParam.get("endMonth");  
	String endYear = reportParam.get("endYear");  
	
	double totalSales = 0;  
	double totalReturns = 0; 
	double totalExchanges = 0; 
 
	Report r = new Report();
	HashMap<String, String> productNames = r.getProductNames(); 
    HashMap<String, ArrayList<Double>> salesData = r.getData(startDate, startMonth, startYear, endDate, endMonth, endYear, "Purchase"); 
	
	Set<String> sdKeys = salesData.keySet(); 
	TreeSet<String> tableRows = new TreeSet<String>(); 
	Iterator itr = sdKeys.iterator(); 	
	while (itr.hasNext()) {
		String key = (String) itr.next(); 
		ArrayList<Double> prodData = salesData.get(key); 
		double quantity = prodData.get(0); 
		double price = prodData.get(1); 
		totalSales += (price * quantity); 
		String tableLine = "<tr><td id='pid'>" + key + "</td><td id='prodDesc'>" + productNames.get(key) + "</td><td id='quantity'>" + quantity + "</td><td id='price'>" + roundTwoDecimals(price) + "</td><td id='total'>" + roundTwoDecimals(quantity * price) + "</td></tr>"; 
		tableRows.add(tableLine); 
	}
	
	//get exchange data
	HashMap<String, ArrayList<Double>> exchangeData = r.getData(startDate, startMonth, startYear, endDate, endMonth, endYear, "Exchange"); 
	
	Set<String> edKeys = exchangeData.keySet(); 
	TreeSet<String> exTableRows = new TreeSet<String>(); 
	Iterator itr3 = edKeys.iterator(); 	
	while (itr3.hasNext()) {
		String key = (String) itr3.next(); 
		ArrayList<Double> prodData = salesData.get(key); 
		double quantity = prodData.get(0); 
		double price = prodData.get(1); 
		totalExchanges += (price * quantity); 
		String tableLine = "<tr><td id='pid'>" + key + "</td><td id='prodDesc'>" + productNames.get(key) + "</td><td id='quantity'>" + quantity + "</td><td id='price'>" + roundTwoDecimals(price) + "</td><td id='total'>" + roundTwoDecimals(quantity * price) + "</td></tr>"; 
		exTableRows.add(tableLine); 
	}
	
	//get return data
	HashMap<String, ArrayList<Double>> returnData = r.getData(startDate, startMonth, startYear, endDate, endMonth, endYear, "Return"); 
	
	Set<String> rdKeys = returnData.keySet(); 
	TreeSet<String> rdTableRows = new TreeSet<String>(); 
	Iterator itr4 = rdKeys.iterator(); 	
	while (itr4.hasNext()) {
		String key = (String) itr4.next(); 
		ArrayList<Double> prodData = salesData.get(key); 
		double quantity = prodData.get(0); 
		double price = prodData.get(1); 
		totalReturns += (price * quantity); 
		String tableLine = "<tr><td id='pid'>" + key + "</td><td id='prodDesc'>" + productNames.get(key) + "</td><td id='quantity'>" + quantity + "</td><td id='price'>" + roundTwoDecimals(price) + "</td><td id='total'>" + roundTwoDecimals(quantity * price) + "</td></tr>"; 
		rdTableRows.add(tableLine); 
	}
	
	//output
	
	String dateRange = startDate + "-" + startMonth + "-" + startYear + " to " + endDate + "-" + endMonth + "-" + endYear; 
	out.println("<h2>Sales Report</h2><p>From " + dateRange +"</p>");
	
	out.println("<p>Summary: </p><ul><li>Total Sales: " + roundTwoDecimals(totalSales) + "</li><li>Total Returns: " + roundTwoDecimals(totalReturns) +"</li><li>Total Exchanges: " + roundTwoDecimals(totalExchanges) + "</li><li>Revenue: " + roundTwoDecimals(totalSales - totalReturns) + "</li></ul>");
	
	//outputs sales
	out.println("<h3>Sales:</h3>");
	Iterator itr2 = tableRows.iterator();
	out.println("<table cellpadding='2' border='1'> <col span='10' /> <col span='2'/> <col span='3'/> <col span='3'/>"); 
	out.println("<tr><td>PID</td><td>Product Description</td><td>Quantity</td><td>Price</td><td>Total</td></tr>"); 
	while (itr2.hasNext()) {
		String currentLine = (String) itr2.next(); 
		out.println(currentLine); 
	}
	out.println("</table>"); 
	
	//outputs returns
	out.println("<br/><h3>Returns:</h3>"); 
	Iterator itr5 = rdTableRows.iterator();
	out.println("<table cellpadding='1' border='1'> <col span='5' /> <col span='2'/> <col span='3'/> <col span='3'/>"); 
	out.println("<tr><td>PID</td><td>Product Description</td><td>Quantity</td><td>Price</td><td>Total</td></tr>"); 
	while (itr5.hasNext()) {
		String currentLine = (String) itr5.next(); 
		out.println(currentLine); 
	}
	out.println("</table>"); 
	
	
	//outputs exchanges
	out.println("<br/><h3>Exchanges:</h3>"); 
	Iterator itr6 = exTableRows.iterator();
	out.println("<table cellpadding='1' border='1'> <col span='5' /> <col span='2'/> <col span='3'/> <col span='3'/>"); 
	out.println("<tr><td>PID</td><td>Product Description</td><td>Quantity</td><td>Price</td><td>Total</td></tr>"); 
	while (itr6.hasNext()) {
		String currentLine = (String) itr6.next(); 
		out.println(currentLine); 
	}
	out.println("</table>"); 
	
	r.closeConnection(); 


%>


</body>
</html>